﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using GC2.Entities.Seguridad;
using GC2.ExceptionHandler;
using System.Text;

/// <summary>
/// Descripción breve de GenericUserControls
/// </summary>
public class GenericUserControl : System.Web.UI.UserControl
{
    private ExceptionConstructor exNew = new ExceptionConstructor();

    private string connection = "";
    public string Connection
    {
        get { return connection; }
        set { connection = value; }
    }

    public GenericUserControl()
    {
  
    }

    #region SetearCultura
    public void SetearCultura()
    {
        try
        {
            System.Globalization.CultureInfo cultura = new System.Globalization.CultureInfo("es-AR");
            cultura.NumberFormat.NumberDecimalSeparator = ".";
            cultura.NumberFormat.NumberGroupSeparator = ",";
            cultura.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            System.Threading.Thread.CurrentThread.CurrentCulture = cultura;
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region SetearConexion
    public void SetearConexion()
    {
        try
        {
            if (Session["UsuarioActual"] != null)
            {
                Connection = ((Usuarios)Session["UsuarioActual"]).Cliente;
            }
        }
        catch (Exception ex) { ErrorUI(ex); }
    }
    #endregion

    #region Manejo de Excepciones
    /// <ErrorUI>
    /// Rutina de Error para Interfaz de Usuario
    /// CB 06-07-2007
    /// </ErrorUI>
    /// <param name="ex"></param>
    public void ErrorUI(Exception ex)
    {
        ExceptionConstructor exConstructor = new ExceptionConstructor();
        string exTemp = ex.ToString();
        string control = (ex.ToString()).Remove(33);
        string InfoError;

        if (control == "System.Exception:  -- > Original ")
        {
            InfoError = exConstructor.exBus(exTemp);
        }
        else
        {
            StringBuilder sb = new StringBuilder();

            sb.Append(" -- > Original Exception: User Interface");
            sb.Append(Environment.NewLine);
            sb.Append(ex.ToString());
            sb.Append(Environment.NewLine);
            sb.Append((exNew.ProcessUnhandledException(ex)).ToString());

            InfoError = sb.ToString();
        }

        Session["Error"] = InfoError;
        Session["UrlOrigen"] = "~/BMSeguridad/Default.aspx";
        Response.Redirect("~/Error.aspx", false);
    }
    #endregion
}
